package com.example.culturemanage.culture

import android.util.Log
import android.view.View
import android.widget.SearchView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.culturemanage.CultureManageViewModel
import com.example.culturemanage.R
import com.example.culturemanage.adapter.CultureManageAdapter
import com.example.culturemanage.adapter.CulturesearchAdapter
import com.example.culturemanage.databinding.ActivityCultureManageBinding
import com.example.culturemanage.entity.SearchEntityEntity
import com.example.culturemanage.entity.SearchEntityItem
import com.example.lib_base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint
import android.view.inputmethod.InputMethodManager
import android.content.Context

@AndroidEntryPoint
@Route(path = "/culturemanage/CultureManageActivity")
class CultureManageActivity : BaseActivity<ActivityCultureManageBinding, CultureManageViewModel>() {
    private lateinit var adapterCulture: CultureManageAdapter
    private lateinit var searchadapter: CulturesearchAdapter
    override fun initView() {
        binding.back.setOnClickListener { finish() }
        binding.add.setOnClickListener {
            ARouter.getInstance().build("/culturemanage/AddCultureActivity").navigation()
        }
        // 初始化两个适配器和布局
        adapterCulture = CultureManageAdapter()
        searchadapter = CulturesearchAdapter()
        binding.wenhuarv.adapter = adapterCulture
        binding.wenhuarv.layoutManager = LinearLayoutManager(this)
        binding.searchrv.adapter = searchadapter
        binding.searchrv.layoutManager = LinearLayoutManager(this)
        // 默认只显示正常列表
        binding.wenhuarv.visibility = View.VISIBLE
        binding.searchrv.visibility = View.GONE
        vm.CultureList()

        adapterCulture.addChildClickViewIds(R.id.btnDetail)
        adapterCulture.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.btnDetail->{
                    ARouter.getInstance().build("/culturemanage/DetailCultureActivity")
                        .withInt("homeid",adapterCulture.data[position].id).navigation()
                }
            }
        }
        // 搜索
        binding.seach.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (!query.isNullOrEmpty()) {
                    vm.searchCulture(query)
                    // 隐藏软键盘
                    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(binding.seach.windowToken, 0)
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()) {
                    // 搜索框清空，恢复正常列表
                    binding.wenhuarv.visibility = View.VISIBLE
                    binding.searchrv.visibility = View.GONE
                    searchadapter.data.clear() // 清空搜索结果
                    searchadapter.notifyDataSetChanged()
                }
                return false
            }
        })

        // 左滑删除
        val helper = ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0, left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.layoutPosition
                vm.delCulture(adapterCulture.data[position].id)
                adapterCulture.data.removeAt(position)
                adapterCulture.notifyDataSetChanged()
            }
        })
        helper.attachToRecyclerView(binding.wenhuarv)
    }

    override fun initData() {
        vm.CultureListSuccess.observe(this) {
            adapterCulture.data.clear()
            adapterCulture.data.addAll(it)
            adapterCulture.notifyDataSetChanged()
            // 默认显示正常列表，隐藏搜索结果
            binding.wenhuarv.visibility = View.VISIBLE
            binding.searchrv.visibility = View.GONE
        }
        vm.delCultureSuccess.observe(this) {
            ToastUtils.showShort("删除成功")
            Log.d("del", "initData: $it")
            adapterCulture.notifyDataSetChanged()
        }
        vm.searchCultureSuccess.observe(this) {
            searchadapter.data.clear() // 先清空
            searchadapter.data.addAll(it)
            searchadapter.notifyDataSetChanged()
            // 搜索成功时只显示搜索结果
            binding.wenhuarv.visibility = View.GONE
            binding.searchrv.visibility = View.VISIBLE
        }
    }

    override fun onResume() {
        super.onResume()
        vm.CultureList()
    }
}